
/*

Input: newdata/estimation_prep_ltw18 [prepared precinct-level panel]

Output: Figure D.9	

Task: Heterogeneity by distance change, where changes are consistent for at least
		90% of addresses or polling locations barely changed

*/	
	
* PULL: Precinct-level data
	use "$newdata/estimation_prep_ltw18.dta", clear
	
	
********************************************************************************
*	 Prep Estimation *
********************************************************************************

	// relabel outcomes 
	lab var turnout_urne 	"Polling Place Turnout"
	lab var turnout_pos_req "Mail-in Turnout"
	lab var turnout_tot_req "Total Turnout"	
	
// DEFINE sample, where treatm changed distance in SAME direction for >90% of units
	gen smpl_ambi=0
	gen frac_inc = street_increased/treat_simple			// share del_street_dist >0
	gen frac_dec = street_decreased/treat_simple			// share del_street_dist <0
	gen d_in  = (frac_inc>.90 | frac_dec>.90) if K==0		// I(shares > 90%)
	bys sb_new (d_in): replace smpl_ambi=d_in[1]
	replace smpl_ambi = 1 if missing(Ei)
	lab var smpl_ambi "sample def excluding units with ambiguous distance change for HHs within precinct"
	
	// compute dummies for DISTANCE increase/decrease 90%+ AND 3rd cat. for dist(WL_t, WL_t-1) small
	cap drop tmp*
	gen 	tmp = (del_street_dist>0 & d_in==1) 			if K==0
	bys sb_new (tmp): gen ind_dist_up = tmp[1]
	replace ind_dist_up = 0 					if missing(Ei)
	lab var ind_dist_up "=1 if dist increase 90%+"
	
	cap drop tmp*
	gen 	tmp = (del_street_dist<0 & d_in==1) 			if K==0
	bys sb_new (tmp): gen ind_dist_dn = tmp[1]
	replace ind_dist_dn = 0 					if missing(Ei)
	lab var ind_dist_dn "=1 if dist decrease 90%+"
	
	cap drop tmp*
	gen 	tmp = (wl_street_dist<.8 & d_in==0) 			if K==0
	bys sb_new (tmp): gen ind_dist_sm = tmp[1]
	replace ind_dist_sm = 0 					if missing(Ei)
	lab var ind_dist_sm "=1 if dist changed little"	
	
	tab ind_dist_up ind_dist_dn  if K==0	
	tab ind_dist_sm if K==0
	assert ind_dist_sm==0 if ind_dist_up==1| ind_dist_dn==1
	
	*>>> Adj. sample to include SMALL dist changes<<<
	replace smpl_ambi = 1 if ind_dist_sm==1

		
********************************************************************************
* Het: 3 groups, 90%+ HH dist UP, 90%# HH DOWN, dist[WL_t, WL_(t-1)]=small (Figure D9)
********************************************************************************			
	
	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen F`l'event = K==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event = K==`l'
	}
	
	// Create two set of dummies: Reason Dummy x rel. time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a = F`l'event *ind_dist_dn	// a := decrease
		gen	 	F`l'event_b = F`l'event *ind_dist_sm	// b:= middle
		gen 	F`l'event_c = F`l'event *ind_dist_up	// c := increase
		assert  F`l'event_b+F`l'event_a+F`l'event_c==F`l'event	if smpl_ambi==1	
		lab var F`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		lab var F`l'event_b "(N0)x\hspace{.7cm}Reassignment (#t-`l'#)"
		lab var F`l'event_c "(N+)x\hspace{.7cm}Reassignment (#t-`l'#)"
		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a = L`l'event *ind_dist_dn	// a := decrease
		gen	 	L`l'event_b = L`l'event *ind_dist_sm	// b:= middle
		gen 	L`l'event_c = L`l'event *ind_dist_up	// c := increase
		assert  L`l'event_b+L`l'event_a+L`l'event_c==L`l'event	if smpl_ambi==1	
		lab var L`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		lab var L`l'event_b "(N0)x\hspace{.7cm}Reassignment (#t+`l'#)"
		lab var L`l'event_c "(N+)x\hspace{.7cm}Reassignment (#t+`l'#)"		
		
	}		
	
	// ORDER dummies
	order *event_b, last
	order *event_c, last
	order F1event*,last	
	// Label for figure header
	lab var turnout_urne 	"{bf:Panel B.} Effect on Polling Place Turnout"
	lab var turnout_pos_req "{bf:Panel C.} Effect on Mail-in Turnout"
	lab var turnout_tot_req "{bf:Panel D.} Effect on Total Turnout"
	lab var del_street_dist "{bf:Panel A.} Change in Distance"	
	
	
	// Estimate ES by increase/decrease in distance
	estimates clear
	outreg, clear
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req del_street_dist treat_simple{
	
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F7event_c-L7event_c F1event_a F1event_b F1event_c ///
				$ctr $wgt if smpl_trim ==1 & smpl_ambi==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)		
				qui outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b F4event_c-L2event_c) store(`v')

		estimates store `v'_a
		estimates store `v'_b	
		estimates store `v'_c	

		
		// PLOT 
		event_plot  `v'_a `v'_b `v'_c, ///
		stub_lag(L#event_a L#event_b L#event_c ) stub_lead(F#event_a F#event_b F#event_c) plottype(scatter) ciplottype(rcap) ///
		together perturb(-0.15(0.15)0.15) trimlead(4) trimlag(2) noautolegend ///
		graph_opt(xtitle("") ytitle("Voter turnout in %""(estimates)", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment") ///
			legend(pos(12) order(1 "Distance decrease for >90% addresses" 3 "Polling location moved <800m" 5 "Distance increase for >90% addresses" ) row(1) region(style(none)) bexpand span justification(left)) ///
			xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lpat(solid) lcol(gray)) ylabel(, angle(horizontal)) ///
			title("`:var lab `v''", nobox span bexpand justification(left) size(medium)) ///
			name(`v', replace)) ///
		lag_opt1(msymbol(O) msize(2.5pt) color(black)) 	lag_ci_opt1(color(black)) ///
		lag_opt2(msymbol(Dh) msize(2.5pt) color(gray)) lag_ci_opt2(color(gray)) ///
		lag_opt3(msymbol(S) msize(2.5pt) color(cranberry)) lag_ci_opt3(color(cranberry)) 	 		 	

	}
	
	* PLOT: FIGURE D9. Effect Heterogeneity by Change in Proximity Restricted to Cases with Consistent Distance Changes	
	grc1leg del_street_dist turnout_urne turnout_pos_req turnout_tot_req, col(2)  xcommon pos(6) imargins(small)
	gr_edit .plotregion1.graph1.yaxis1.reset_rule -0.6 0.6 .3 , tickset(major) ruletype(range) 
	gr_edit .plotregion1.graph1.yaxis1.title.text = {}
	gr_edit .plotregion1.graph1.yaxis1.title.text.Arrpush `"Change in distance in km"'
	gr_edit .plotregion1.graph1.yaxis1.title.text.Arrpush `"(estimates)"'
	gr_edit .style.editstyle declared_ysize(4) editcopy
	graph export "$figures/Figure_D9_ES_het_by_distance_ambi.pdf", replace	
